require "rubygems"

# The actual dsl
module Amadeusdsl
    
    module Parser
      extend self

        # does this command expect a code block?
        def isBlockCommand(l)
          commands = ["Find", "Passenger", "Search", "Connection", "Book", "Hold", "Seats", "Days", "Cancel","Route","Check","Destinations","Flights","Airlines","Airports","Shortest","Cheapest","Search","Select"]
          commands.each do
            |c| 
            if (l.include? c) 
              return true 
            end
          end
          return false
        end

        def parse(file) 
            string = ""
            dos = 0
            file.each_line do 
              |line|
              line = line.chomp
              if (line[0,1] != "#") 
              if (isBlockCommand(line))
                string << line << ("\t") << ("do \n")
                dos += 1
              else if (dos > 0 && /\S/ !~ line)
                  string << "end \n \n"
                  dos -= 1
                else
                  string << line << "\n"
                end
              end
            end
            end
            if (dos > 0)
              for i in 0..dos-1
                string += "end \n"
              end
            end
         #   puts string
         #   puts "----------------END PARSED --------------"

            return string
        end

        # method missing what do ?
        def method_missing(sym, *args, &block)
            puts "# unknown #{sym} with args #{args} #{block_given? ? '' : 'and code'}"
        end
    end
end
